package com.dragon7531.bdemo.controller;

import com.dragon7531.bdemo.domain.Student;
import com.dragon7531.bdemo.exception.BusinessException;
import com.dragon7531.bdemo.service.BaccountService;
import com.dragon7531.bdemo.service.StudentService;
import com.dragon7531.bdemo.utils.HttpCodeEnum;
import com.dragon7531.bdemo.utils.ResponseResult;
import com.dragon7531.bdemo.validate.InsertValidateGroup;
import com.dragon7531.bdemo.validate.UpdateValidateGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.NumberUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
@Validated
public class StudentController {

    @Autowired
    private StudentService studentService;
    @RequestMapping("/insert-get")
    public ResponseResult insertForGetStudent(Student student) {
        studentService.insert(student);
        return ResponseResult.okResult();
    }
    @RequestMapping("/insert")
    public ResponseResult insertStudent(@RequestBody Student student) {
        String name = student.getName();
        StringBuilder stringBuilder = new StringBuilder();
        if(!StringUtils.hasText(name)){
            System.out.println("姓名不能为空");
            //throw new RuntimeException("姓名不能为空");
            stringBuilder.append("姓名不能为空");

        }
        int nameLength = name.length();
        if(nameLength<2 || nameLength>20){
            System.out.println("姓名长度应在2到19之间");
            //throw new RuntimeException("姓名长度应在2到19之间");
            stringBuilder.append("姓名长度应在2到19之间").append(";");
        }
        Integer age = student.getAge();
        if(age==null){
            System.out.println("年龄不能为空");
            //throw new RuntimeException("年龄不能为空");
            stringBuilder.append("年龄不能为空");
        }
        if(age<3 || age>150){
            System.out.println("年龄应大于3岁小于150岁");
            //throw new RuntimeException("年龄应大于3岁小于150岁");
            stringBuilder.append("年龄应大于3岁小于150岁");
        }
        if(StringUtils.hasText(stringBuilder.toString())) {
            return ResponseResult.errorResult(500, stringBuilder.toString());
        }else {
            studentService.insert(student);
            return ResponseResult.okResult();
        }
    }
    @RequestMapping("/update")
    public String updateStudent(@RequestBody Student student) {
        Integer id = student.getId();
        if(null==id){
            throw new RuntimeException("id不能为空");
        }
        String name = student.getName();
        if(!StringUtils.hasText(name)){
            System.out.println("姓名不能为空");
            throw new RuntimeException("姓名不能为空");
        }
        int nameLength = name.length();
        if(nameLength<1 || nameLength>20){
            System.out.println("姓名长度应在2到19之间");
            throw new RuntimeException("姓名长度应在2到19之间");
        }
        Integer age = student.getAge();
        if(age==null){
            System.out.println("年龄不能为空");
            throw new RuntimeException("年龄不能为空");
        }
        if(age<3 || age>150){
            System.out.println("年龄应大于3岁小于150岁");
            throw new RuntimeException("年龄应大于3岁小于150岁");
        }
        studentService.update(student);
        return "成功";
    }
    @RequestMapping("/insert2")
    public ResponseResult insertStudent1(@Validated @RequestBody Student student) {
        studentService.insert(student);
        return ResponseResult.okResult();
    }
    @PostMapping("/insertForPost")
    public ResponseResult insertForPost(@Validated @RequestBody Student student) {
        studentService.insert(student);
        return ResponseResult.okResult();
    }
    @GetMapping("/insertGet")
    public ResponseResult insertForGet(@RequestParam String name) {
        //studentService.insert(student);
        return ResponseResult.okResult();
    }
    @PostMapping("/insertPostValid")
    public ResponseResult insertPostValid(@Validated @RequestBody Student student) {
        studentService.insert(student);
        return ResponseResult.okResult();
    }
    @PostMapping("/insertPostBusiness")
    public ResponseResult insertPostBusiness(@Validated @RequestBody Student student) {
        if(student.getName().equals("lisi")) {
            throw new BusinessException(HttpCodeEnum.USERNAME_EXIST);
        }
        return ResponseResult.okResult();
    }
    @PostMapping("/insertPostRuntime")
    public ResponseResult insertPostRuntime(@Validated @RequestBody Student student) {
        if(student.getName().equals("wangwu")) {
            int aa=1/0;
        }
        return ResponseResult.okResult();
    }
    @RequestMapping("/update2")
    public ResponseResult updateStudent2(@Validated(UpdateValidateGroup.class)  @RequestBody Student student) {
        studentService.update(student);
        return ResponseResult.okResult();
    }

}
